package cn.itcast.star.graph.core.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import cn.itcast.star.graph.comfyui.client.api.ComfyuiApi;
import cn.itcast.star.graph.comfyui.client.pojo.ComfyuiModel;
import cn.itcast.star.graph.comfyui.client.pojo.ComfyuiRequestDto;
import cn.itcast.star.graph.comfyui.client.pojo.ComfyuiTask;
import cn.itcast.star.graph.core.pojo.dto.request.Text2ImageResDto;
import cn.itcast.star.graph.core.pojo.dto.respone.Text2ImageReqDto;
import cn.itcast.star.graph.core.service.FreemarkerService;
import cn.itcast.star.graph.core.service.OllamaService;
import cn.itcast.star.graph.core.service.RedisService;
import cn.itcast.star.graph.core.service.Text2ImageService;
import com.alibaba.fastjson2.JSON;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author Windows
 */
@Service
public class Text2ImageServiceImpl implements Text2ImageService {

    public static final String COMFYUI_CLIENT_ID = "star-graph";

    @Resource
    ComfyuiApi comfyuiApi;
    @Resource
    OllamaService ollamaService;
    @Resource
    FreemarkerService freemarkerService;
    @Resource
    RedisService redisService;

    /**
     * 获取Comfyui任务
     * @param text2ImageResDto
     * @return
     * @throws Exception
     */
    public ComfyuiTask getComfyuiTask(Text2ImageResDto text2ImageResDto) throws Exception {
        // 数据转换
        ComfyuiModel model = BeanUtil.toBean(text2ImageResDto, ComfyuiModel.class);
        model.setWidth(text2ImageResDto.width());
        model.setHeight(text2ImageResDto.height());
        model.setSamplerName(text2ImageResDto.samplerName());
        model.setScheduler(text2ImageResDto.scheduler());
        model.setModelName(text2ImageResDto.modelName());

        // 翻译
        model.setPropmt(String.format("(8k, best quality, masterpiece),(high detailed skin),%s", ollamaService.translate(model.getPropmt())));
        if(StrUtil.isNotEmpty(model.getReverse())){
            model.setReverse(ollamaService.translate(model.getReverse()));
        }
        model.setReverse(String.format("%s,bad face,naked,bad finger,bad arm,bad leg,bad eye", model.getReverse()));

        // 模版生成json字符串
        String prompt = freemarkerService.renderText2Image(model);

        // 封装请求Comfyui的请求参数
        ComfyuiRequestDto dto = new ComfyuiRequestDto(COMFYUI_CLIENT_ID, JSON.parseObject(prompt));

        // 封装task任务并返回
        ComfyuiTask task = new ComfyuiTask(text2ImageResDto.getClientId(), dto);
        return task;
    }


    @Override
    public Text2ImageReqDto propmt(Text2ImageResDto text2ImageResDto) {
        try {
            ComfyuiTask comfyuiTask =getComfyuiTask(text2ImageResDto);
            comfyuiTask=redisService.addQueueTask(comfyuiTask);
            Text2ImageReqDto text2ImageReqDto = new Text2ImageReqDto();
            text2ImageReqDto.setPid(comfyuiTask.getId());
            text2ImageReqDto.setQueueIndex(comfyuiTask.getIndex());
            return text2ImageReqDto;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
